package com.hundredhealth.rose.util;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import com.hundredhealth.rose.HealthLabelApp;
import com.hundredhealth.rose.model.PrintLabel;
import com.lvrenyang.io.Label;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class Prints {

    public static boolean PrintTicket(PrintLabel printLabel, int nPrintWidth, int nPrintHeight) {
        Label label = new Label();
        label.Set(HealthLabelApp.mBTPrinting);

        boolean bPrintResult = false;

        int w = nPrintWidth;
        int h = nPrintHeight;

//        Bitmap logo = getImageFromAssetsFile(ctx, "Kitty.bmp");

        label.PageBegin(0, 0, w, h, 1);
//        label.DrawBox(0, 0, w-1, h-1, 1, 1);
//        label.DrawBitmap(0, 0, logo.getWidth(), logo.getHeight(), 0, logo, 0);
        try {
            String[] infoText = new String[]{printLabel.getName(), printLabel.getSex(), printLabel.getAge()};
            int infoMaxX = 300;
            int infoX = (w - infoMaxX) / 2;
            int spaceCount = (int) ((infoMaxX * 25.4D / 203.0D - getWidth(infoText[0] + infoText[1] + infoText[2])) / 3 / getWidth("a"));
            String infoString = infoText[0] + StringUtils.repeat(" ", spaceCount) + infoText[1] + StringUtils.repeat(" ", spaceCount) + infoText[2];
            label.DrawPlainText(infoX, 10, 24, 0, infoString.getBytes("GBK"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        label.DrawBarcode(20, 40, 8, 80, 2, 0, "*3201010170010001*".getBytes());
//        label.DrawQRCode(200, 50, 0, 1, 4, 0, "CaysnPrinter".getBytes());

        label.PageEnd();
        label.PagePrint(printLabel.getPrintCount());

        bPrintResult = label.GetIO().IsOpened();

        return bPrintResult;
    }

    /**
     * 从Assets中读取图片
     */
    public static Bitmap getImageFromAssetsFile(Context ctx, String fileName) {
        Bitmap image = null;
        AssetManager am = ctx.getResources().getAssets();
        try {
            InputStream is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return image;
    }

    public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
        // load the origial Bitmap
        Bitmap BitmapOrg = bitmap;

        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = w;
        int newHeight = h;

        // calculate the scale
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the Bitmap
        matrix.postScale(scaleWidth, scaleHeight);
        // if you want to rotate the Bitmap
        // matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                height, matrix, true);

        // make a Drawable from Bitmap to allow to set the Bitmap
        // to the ImageView, ImageButton or what ever
        return resizedBitmap;
    }

    private static int getWidth(String str) {
        int width = 0;
        char[] ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (CharUtil.isChinese(c)) {
                width += 24;
            } else {
                width += 12;
            }
        }
        return ((int) (width / 203.0D * 25.4D * 1.0D));
    }
}
